package com.taxi.driver.module.account.firstlogin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qianxx.utils.TypeUtils;
import com.taxi.driver.R;
import com.taxi.driver.common.AppConfig;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.data.entity.DriverEntity;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.event.UIEvent;
import com.taxi.driver.module.login.LoginActivity;
import com.taxi.driver.widget.CircleImageView;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：首次登录成功后，司机信息展示页
 */
public class FirstLoginActivity extends BaseActivity {

    @Inject
    UserRepository mUserRepository;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, FirstLoginActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_notice)
    TextView mTvNotice;
    @BindView(R.id.iv_avatar)
    CircleImageView mIvAvatar;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_car_number)
    TextView mTvCarNumber;
    @BindView(R.id.tv_cat_tag)
    TextView mTvCatTag;
    @BindView(R.id.tv_car_info)
    TextView mTvCarInfo;
    @BindView(R.id.tv_relogin)
    TextView mTvRelogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_login);
        ButterKnife.bind(this);
        Application.getAppComponent().inject(this); //依赖注入
        setUserInfo(mUserRepository.getTempDriverEntity()); //设置显示
    }

    @OnClick(R.id.tv_relogin)
    public void onClick() {
        reLogin();
    }

    public void setUserInfo(DriverEntity vo) {
        if (vo == null) return;
        Glide.with(this).load(TypeUtils.getValue(vo.face)).into(mIvAvatar);
        mTvName.setText(TypeUtils.getValue(vo.name));
        mTvCarNumber.setText(TypeUtils.getValue(vo.plateNum));
//        mTvCatTag.setText(""); //TODO 需要服务端返回相应字段
//        mTvCatTag.setVisibility(View.GONE);

        if (AppConfig.isTaxi()) { //出租车，显示所属城市信息
            mTvCarInfo.setText(TypeUtils.getValue(vo.shortName));

        } else { //非出租车，显示车辆信息
            StringBuilder str = new StringBuilder();
            String color = TypeUtils.getValue(vo.carColor);
            String brandName = TypeUtils.getValue(vo.brandName);
            if (!TextUtils.isEmpty(color)) {
                str.append(color);
                str.append("•");
            }
            str.append(brandName);
            mTvCarInfo.setText(str.toString());
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        reLogin();
    }

    private void reLogin() {
        EventBus.getDefault().post(new UIEvent(UIEvent.CLEAR_PWD)); //发送通知，清空登录页密码
        mUserRepository.setTempDriverEntity(null); //清空临时用户数据
        LoginActivity.start(this);
    }
}
